package com.example.androidproject.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.bumptech.glide.Glide;
import com.example.androidproject.app.classifybean.ClassifyBean;
import com.example.androidproject.movie.bean.ResultBeanData;
import com.example.androidproject.myview.MyGridView;
import com.example.androidproject.myview.MyListView;
import com.example.androidproject.novel.bean.NovelBean;
import com.example.androidproject.R;
import com.example.androidproject.wallpaper.bean.WallpaperBean;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 *
 */
public class ClassifyAdapter3 extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final String TAG = "GGSTUDY";
    private final Context mContext;
    private final int classifyFlag;
    private final LayoutInflater layoutInflater;
    private List<WallpaperBean.DataBean> wallpaperList;
    private List<ClassifyBean.DataBean> clssifyList;
    private List<ResultBeanData.DataBean> movieList;
    private List<NovelBean.DataBean> novellist;
    private OnItemClickListener myClickListener;
    private OnItemClickListenerNav myItemClickListenerNav;
    private onAllBtnClickListener myAllBtnClick;

    private final int ITEM_NAV = 1;
    private final int ITEM_CONTENT = 2;
    CountDownLatch latch = new CountDownLatch(1);
    public int clickpostion = -1;
    public ClassifyOptionsAdapter classifyOptionsAdapter;

    public ClassifyAdapter3(Context mContext, List<ResultBeanData.DataBean> movielist, int classifyFlag) {
        this.mContext = mContext;
        this.classifyFlag = classifyFlag;
        layoutInflater = LayoutInflater.from(mContext);
    }

    public ClassifyAdapter3(Context applicationContext, Object list, int classifyFlag, List<ClassifyBean.DataBean> classifyList) {
        this.mContext = applicationContext;
        this.classifyFlag = classifyFlag;
        this.clssifyList = classifyList;
        if (classifyFlag == 1) {
            this.movieList = (List<ResultBeanData.DataBean>) list;
        }
        if (classifyFlag == 2) {
            this.novellist = (List<NovelBean.DataBean>) list;
        }
        if (classifyFlag == 3) {
            this.wallpaperList = (List<WallpaperBean.DataBean>) list;
        }
        layoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridLayoutManager = (GridLayoutManager) manager;
            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case ITEM_NAV:
                            return 9;
                        case ITEM_CONTENT:
                            return 3;
                        default:
                            return 3;
                    }
                }
            });
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ITEM_NAV) {
            return new OptionViewHolder(mContext, layoutInflater.inflate(R.layout.classify_options_laytout, null));
        } else {
            if (classifyFlag == 1) {
                return new ContentViewHolder(layoutInflater.inflate(R.layout.classify3_item, null));
            }
            if (classifyFlag == 2) {
                return new ContentViewHolder(layoutInflater.inflate(R.layout.classify3_novel_item, parent, false));
            }
            if (classifyFlag == 3) {
                return new ContentViewHolder(layoutInflater.inflate(R.layout.classify3_item, null));
            }
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof OptionViewHolder) {
            OptionViewHolder optionViewHolder = (OptionViewHolder) holder;
            optionViewHolder.setData(position);
        }
        if (holder instanceof ContentViewHolder) {
            ContentViewHolder contentViewHolder = (ContentViewHolder) holder;
            contentViewHolder.setData(position - 1);
        }
    }

    @Override
    public int getItemCount() {
        if (classifyFlag == 1) {
            if (movieList == null)
                return 0;
            return movieList.size() + 1;
        }
        if (classifyFlag == 2) {
            if (novellist == null)
                return 0;
            return novellist.size() + 1;
        }
        if (classifyFlag == 3) {
            if (wallpaperList == null)
                return 0;
            return wallpaperList.size() + 1;
        }
        return 0;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ITEM_NAV;
        } else {
            return ITEM_CONTENT;
        }
    }

    class ContentViewHolder extends RecyclerView.ViewHolder {
        public final ImageView iv_classify_content;
        public final TextView tv_classify_content, tv_novel_title, tv_novel_introduce, tv_novel_author, tv_novel_num;
        public final CardView gard_novel_body;
        public final LinearLayout ll_body_item;

        public ContentViewHolder(@NonNull View itemView) {
            super(itemView);
            iv_classify_content = itemView.findViewById(R.id.iv_classify_content);
            tv_classify_content = itemView.findViewById(R.id.tv_classify_content);
            tv_novel_title = itemView.findViewById(R.id.tv_novel_title);
            tv_novel_introduce = itemView.findViewById(R.id.tv_novel_introduce);
            tv_novel_author = itemView.findViewById(R.id.tv_novel_author);
            tv_novel_num = itemView.findViewById(R.id.tv_novel_num);
            gard_novel_body = itemView.findViewById(R.id.gard_novel_body);
            ll_body_item = itemView.findViewById(R.id.ll_body_item);
        }

        public void setData(int position) {
            if (classifyFlag == 1) {
                Glide.with(mContext).load(movieList.get(position).getSrc()).into(iv_classify_content);
                tv_classify_content.setText(movieList.get(position).getName());

                ll_body_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myClickListener.onItemClick(ll_body_item, position, classifyFlag);
                    }
                });
            }
            if (classifyFlag == 2) {
                Glide.with(mContext).load(novellist.get(position).getImage()).into(iv_classify_content);
                tv_novel_title.setText(novellist.get(position).getTitle());
                tv_novel_introduce.setText(novellist.get(position).getInfo());
                tv_novel_author.setText(novellist.get(position).getAuthor());
                tv_novel_num.setText(novellist.get(position).getNumber());

                gard_novel_body.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myClickListener.onItemClick(gard_novel_body, position, classifyFlag);
                    }
                });
            }
            if (classifyFlag == 3) {
                Glide.with(mContext).load(wallpaperList.get(position).getSrc()).into(iv_classify_content);
                tv_classify_content.setText(wallpaperList.get(position).getTitle());

                ll_body_item.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        myClickListener.onItemClick(ll_body_item, position, classifyFlag);
                    }
                });
            }
        }
    }

    class OptionViewHolder extends RecyclerView.ViewHolder {
        private final Context mContext;
        private final MyGridView gv_classify_nav1;
        private final TextView btn_classify_options_all1;
        private final LinearLayout ll_classify3_option;

        public OptionViewHolder(Context mContext, View itemView) {
            super(itemView);
            this.mContext = mContext;
            this.btn_classify_options_all1 = itemView.findViewById(R.id.btn_classify_options_all1);
            this.ll_classify3_option = itemView.findViewById(R.id.ll_classify3_option);
            this.gv_classify_nav1 = itemView.findViewById(R.id.gv_classify_nav1);
        }

        public void setData(int position) {
            // 终于解决了 回到顶部 样式会消失的问题， 原因就是当第一个item滚出屏幕是会被赋值为null， 再一次显示的时候会重新执行这里的代码 如果adapter不做条件判断就会再一次new这样就是导致所有的东西都重置
            if (classifyOptionsAdapter == null) {
                classifyOptionsAdapter = new ClassifyOptionsAdapter(mContext, clssifyList, classifyFlag, btn_classify_options_all1);
            } else {
                classifyOptionsAdapter.notifyDataSetChanged();
            }
            gv_classify_nav1.setAdapter(classifyOptionsAdapter);

            // 点击
            gv_classify_nav1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    getOptionsAdapter(classifyOptionsAdapter);
                    btn_classify_options_all1.setBackgroundResource(R.drawable.unselect_btn_css);
                    myItemClickListenerNav.onItemClick(parent, view, position, id);
                }
            });
            // 全部
            btn_classify_options_all1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getOptionsAdapter(classifyOptionsAdapter);
                    myAllBtnClick.onClick(btn_classify_options_all1);
                }
            });
        }
    }

    /**
     * “全部”按钮监听
     */

    public interface onAllBtnClickListener {
        void onClick(TextView btn_classify_options_all1);
    }

    public void setOnAllBtnClickListener(onAllBtnClickListener onAllBtnClickListener) {
        this.myAllBtnClick = onAllBtnClickListener;
    }

    /**
     * 获取 到分类griview的adapter 中的changeselect 目的是为了点击后能能改变颜色
     */

    public void getOptionsAdapter(ClassifyOptionsAdapter optionsAdapter) {
        this.classifyOptionsAdapter = optionsAdapter;
    }

    /**
     * 位置position的 前后比较
     *
     * @param position
     * @return
     */

    public void comparePosition(int position) {
        if (clickpostion != position) {
            clickpostion = position;
        }
    }

    /**
     * item点击事件监听
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position, int classifyFlag);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.myClickListener = onItemClickListener;
    }

    /**
     * 分类选项点击事件监听
     */

    public interface OnItemClickListenerNav {
        void onItemClick(AdapterView<?> parent, View view, int position, long id);
    }

    public void setOnItemClickListenerNav(OnItemClickListenerNav onItemClickListenerNav) {
        this.myItemClickListenerNav = onItemClickListenerNav;
    }

    /**
     * 刷新选项数据
     *
     * @param list
     */
    @SuppressLint("NotifyDataSetChanged")
    public void refreshOption(List<ClassifyBean.DataBean> list) {
        this.clssifyList = list;
        notifyItemChanged(0);
    }

    /**
     * 增加内容数据
     */
    @SuppressLint("NotifyDataSetChanged")
    public void insertItem(Object list) {
        if (classifyFlag == 1) {
//            movieList.addAll((Collection<? extends ResultBeanData.DataBean>) list);
            notifyItemRangeChanged(1, movieList.size() + 1);
//            notifyItemInserted(movieList.size());
        }
        if (classifyFlag == 2) {
//            novellist.addAll((Collection<? extends NovelBean.DataBean>) list);
            notifyItemRangeChanged(1, novellist.size() + 1);
        }
        if (classifyFlag == 3) {
            notifyItemRangeChanged(1, wallpaperList.size() + 1);
        }
    }

}
